#include "../ModelEditorConfig.h"
#include "ControlPanel.h"
#include "../Model/MD2.h"
#include "../Model/ms3d.h"

///////////////////////////////////////////////////////////////////////////////////
ControlPanelPage::ControlPanelPage(ModelEditorMainFrame *pMainFrame)
{
	m_pMainFrame = pMainFrame;
	m_SliderFramesStart = 0;
	m_SliderFramesEnd = 1;

	m_SliderTimeStart = 0;
	m_SliderTimeEnd = 0;

	//addAndMakeVisible(m_pResetButton = new TextButton(L"ResetToDefault"));
	//m_pResetButton->addListener(this);

	//addAndMakeVisible(m_Control_Panel = new PropertyPanel());
	
	//Array <PropertyComponent*> props0;
	//props0.add(new MaterialBooleanProperty("Lighting", "enabled", "disabled", &g_BooleanLighting));
	//props0.add(new MaterialBooleanProperty("depth test", "enabled", "disabled", &g_BooleanDepthTest));
	//props0.add(new MaterialBooleanProperty("depth write", "enabled", "disabled", &g_BooleanDepthWrite));
	//props0.add(new MaterialBooleanProperty("depth Z greater", "enabled", "disabled", &g_BooleanZFuncGreater));
	//props0.add(new MaterialValueFloatProperty("depth bias", &g_DepthBias, -5.0, 5.0, 0.1));
	//
	//props0.add(new MaterialChooseProperty(&g_CullModeContex));

	//props0.add(new MaterialBooleanProperty("Vertex Color", "enabled", "disabled", &g_BooleanVertexColor));
	//props0.add(new MaterialBooleanProperty("Specular", "enabled", "disabled", &g_BooleanSpecular));
	//props0.add(new MaterialBooleanProperty("Line Mode", "enabled", "disabled", &g_BooleanLineMode));
	//
	//props0.add(new MaterialChooseProperty(&g_ColorWriteContex));

	//props0.add(new MaterialBooleanProperty("Fog", "enabled", "disabled", &g_BooleanFogEnable));

	//props0.add(new ColourPropertyComponent("texture factor", Colour(0xFFFFFFFF), &g_TextureFactorColor, true));

    //m_Control_Panel->addSection("Control Property", props0);


    addAndMakeVisible(m_FrameSlider = new Slider(L"Anim Frame Control slider"));
    m_FrameSlider->setTooltip(L"current frame");
    m_FrameSlider->setRange(0, 1, 1);
    m_FrameSlider->setSliderStyle(Slider::LinearHorizontal);
    m_FrameSlider->setTextBoxStyle(Slider::TextBoxLeft, false, 60, 20);
    m_FrameSlider->setColour(Slider::textBoxOutlineColourId, Colour (0xb2818181));
    m_FrameSlider->addListener(this);

    addAndMakeVisible(m_PauseButton = new TextButton(L"Pause button"));
    m_PauseButton->setTooltip(L"Play/Pause");
	m_PauseButton->setButtonText(L"|>");
    m_PauseButton->addListener(this);

    addAndMakeVisible(m_NextFrameButton = new TextButton(L"Next Frame button"));
    m_NextFrameButton->setTooltip(L"Next Frame");
    m_NextFrameButton->setButtonText(L">");
    m_NextFrameButton->addListener(this);

    addAndMakeVisible(m_LastFrameButton = new TextButton(L"Last Frame button"));
    m_LastFrameButton->setTooltip(L"Last Frame");
    m_LastFrameButton->setButtonText(L"<");
    m_LastFrameButton->addListener(this);

    addAndMakeVisible(m_NextAnimButton = new TextButton(L"Next Anim button"));
    m_NextAnimButton->setTooltip(L"Next Anim");
    m_NextAnimButton->setButtonText(L">>");
    m_NextAnimButton->addListener(this);

    addAndMakeVisible(m_LastAnimButton = new TextButton(L"Last Anim button"));
    m_LastAnimButton->setTooltip(L"Last Anim");
    m_LastAnimButton->setButtonText(L"<<");
    m_LastAnimButton->addListener(this);

    addAndMakeVisible(m_AnimChooseComboBox = new ComboBox(L"Choose animation combo box"));
    m_AnimChooseComboBox->setTooltip(L"Choose an animation");
    m_AnimChooseComboBox->setEditableText(false);
    m_AnimChooseComboBox->setJustificationType(Justification::centredLeft);
    m_AnimChooseComboBox->setTextWhenNothingSelected(String::empty);
    m_AnimChooseComboBox->setTextWhenNoChoicesAvailable(L"(no choices)");
    //m_AnimChooseComboBox->addItem (L"Idle", 1);
    //m_AnimChooseComboBox->addItem (L"run", 2);
    //m_AnimChooseComboBox->addItem (L"file", 3);
    //m_AnimChooseComboBox->addItem (L"play", 4);
    m_AnimChooseComboBox->addListener(this);

    addAndMakeVisible(m_AnimChooseLabel = new Label (L"Anim choose label", L"Anim Choose:"));
    m_AnimChooseLabel->setTooltip(L"Choose an animation");
    m_AnimChooseLabel->setFont(Font (15.0000f, Font::plain));
    m_AnimChooseLabel->setJustificationType(Justification::centredLeft);
    m_AnimChooseLabel->setEditable(false, false, false);
    m_AnimChooseLabel->setColour(TextEditor::textColourId, Colours::black);
    m_AnimChooseLabel->setColour(TextEditor::backgroundColourId, Colour(0x0));

    addAndMakeVisible(m_AnimSpeedEditor = new TextEditor(L"Animation Speed editor"));
    m_AnimSpeedEditor->setTooltip(L"Input Animation Speed");
    m_AnimSpeedEditor->setMultiLine(false);
    m_AnimSpeedEditor->setReturnKeyStartsNewLine(false);
    m_AnimSpeedEditor->setReadOnly(false);
    m_AnimSpeedEditor->setScrollbarsShown(false);
    m_AnimSpeedEditor->setCaretVisible(true);
    m_AnimSpeedEditor->setPopupMenuEnabled(false);
    m_AnimSpeedEditor->setText(L"1.0");
	m_AnimSpeedEditor->addListener(this);

    addAndMakeVisible(m_AnimSpeedLabel = new Label (L"Animation Speed label", L"AnimSpeed X"));
    m_AnimSpeedLabel->setTooltip(L"Input Animation Speed");
    m_AnimSpeedLabel->setFont(Font (15.0000f, Font::plain));
    m_AnimSpeedLabel->setJustificationType(Justification::centredLeft);
    m_AnimSpeedLabel->setEditable(false, false, false);
    m_AnimSpeedLabel->setColour(TextEditor::textColourId, Colours::black);
    m_AnimSpeedLabel->setColour(TextEditor::backgroundColourId, Colour(0x0));

	m_FrameSlider->setEnabled(false);
	m_PauseButton->setEnabled(false);
	m_NextFrameButton->setEnabled(false);
	m_LastFrameButton->setEnabled(false);
	m_NextAnimButton->setEnabled(false);
	m_LastAnimButton->setEnabled(false);
	m_AnimChooseComboBox->setEnabled(false);
	m_AnimChooseLabel->setEnabled(false);
	m_AnimSpeedEditor->setEnabled(false);
	m_AnimSpeedEditor->setReadOnly(true);
	m_AnimSpeedLabel->setEnabled(false);

	startTimer(125);
}

ControlPanelPage::~ControlPanelPage()
{
}

void ControlPanelPage::buttonClicked(Button* buttonThatWasClicked)
{
	if( buttonThatWasClicked == m_NextFrameButton )
	{
		CModelEditorConfig::GetInstance()->m_bFrame0Render.set(0);

		if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MD2 )
		{
			if( CModelEditorConfig::GetInstance()->m_CurrentFrameID + 1 > (int)m_SliderFramesEnd )
				CModelEditorConfig::GetInstance()->m_CurrentFrameID = m_SliderFramesStart;
			else
				CModelEditorConfig::GetInstance()->m_CurrentFrameID++;

			CModelEditorConfig::GetInstance()->m_UIStartFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
			CModelEditorConfig::GetInstance()->m_UIEndFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );

			m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentFrameID, false, false);
		}
		else if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MS3D )
		{
			float step = static_cast<float>(m_FrameSlider->getInterval());
			if( CModelEditorConfig::GetInstance()->m_CurrentAnimTime + step > m_SliderTimeEnd )
				CModelEditorConfig::GetInstance()->m_CurrentAnimTime = m_SliderTimeStart;
			else
				CModelEditorConfig::GetInstance()->m_CurrentAnimTime += step;
			CModelEditorConfig::GetInstance()->m_UIStartTime.set( CModelEditorConfig::GetInstance()->m_CurrentAnimTime );
			CModelEditorConfig::GetInstance()->m_UIEndTime.set( CModelEditorConfig::GetInstance()->m_CurrentAnimTime );

			m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentAnimTime, false, false);
		}
	}
	else if( buttonThatWasClicked == m_LastFrameButton )
	{
		CModelEditorConfig::GetInstance()->m_bFrame0Render.set(0);

		if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MD2 )
		{
			if( CModelEditorConfig::GetInstance()->m_CurrentFrameID == m_SliderFramesStart )
				CModelEditorConfig::GetInstance()->m_CurrentFrameID = m_SliderFramesEnd;
			else
				CModelEditorConfig::GetInstance()->m_CurrentFrameID--;
		
			CModelEditorConfig::GetInstance()->m_UIStartFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
			CModelEditorConfig::GetInstance()->m_UIEndFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
	
			m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentFrameID, false, false);
		}
		else if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MS3D )
		{
			float step = static_cast<float>(m_FrameSlider->getInterval());

			if( CModelEditorConfig::GetInstance()->m_CurrentAnimTime - step < m_SliderTimeStart )
				CModelEditorConfig::GetInstance()->m_CurrentAnimTime = m_SliderTimeEnd;
			else
				CModelEditorConfig::GetInstance()->m_CurrentAnimTime -= step;
		
			CModelEditorConfig::GetInstance()->m_UIStartTime.set( CModelEditorConfig::GetInstance()->m_CurrentAnimTime );
			CModelEditorConfig::GetInstance()->m_UIEndTime.set( CModelEditorConfig::GetInstance()->m_CurrentAnimTime );
	
			m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentAnimTime, false, false);
		}
	}
	else if( buttonThatWasClicked == m_PauseButton )
	{
		CModelEditorConfig::GetInstance()->m_bFrame0Render.set(0);
		CModelEditorConfig::GetInstance()->m_bLoopPlayMode = !CModelEditorConfig::GetInstance()->m_bLoopPlayMode;
		
		if( CModelEditorConfig::GetInstance()->m_bLoopPlayMode )
		{
			m_PauseButton->setButtonText(L"||");

			if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MD2 )
			{
				CModelEditorConfig::GetInstance()->m_CurrentFrameID = m_SliderFramesStart;
				CModelEditorConfig::GetInstance()->m_UIStartFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
				CModelEditorConfig::GetInstance()->m_UIEndFrameID.set( m_SliderFramesEnd );
				m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentFrameID, false, false);
			}
			else if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MS3D )
			{
				CModelEditorConfig::GetInstance()->m_CurrentAnimTime = m_SliderTimeStart;
				CModelEditorConfig::GetInstance()->m_UIStartTime.set(CModelEditorConfig::GetInstance()->m_CurrentAnimTime);
				CModelEditorConfig::GetInstance()->m_UIEndTime.set(m_SliderTimeEnd);
				m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentAnimTime, false, false);
			}
			m_FrameSlider->setEnabled(false);
			m_NextFrameButton->setEnabled(false);
			m_LastFrameButton->setEnabled(false);
			m_NextAnimButton->setEnabled(false);
			m_LastAnimButton->setEnabled(false);
			m_AnimChooseComboBox->setEnabled(false);
			m_AnimChooseLabel->setEnabled(false);
			m_AnimSpeedEditor->setEnabled(false);
			m_AnimSpeedEditor->setReadOnly(true);
			m_AnimSpeedLabel->setEnabled(false);		
		}
		else
		{
			m_PauseButton->setButtonText(L"|>");

			if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MD2 )
			{
				CModelEditorConfig::GetInstance()->m_UIStartFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
				CModelEditorConfig::GetInstance()->m_UIEndFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
				m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentFrameID, false, false);
			}
			else if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MS3D )
			{
				CModelEditorConfig::GetInstance()->m_UIStartTime.set( CModelEditorConfig::GetInstance()->m_CurrentAnimTime );
				CModelEditorConfig::GetInstance()->m_UIEndTime.set( CModelEditorConfig::GetInstance()->m_CurrentAnimTime );
				m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentAnimTime, false, false);
			}
			m_FrameSlider->setEnabled(true);
			m_NextFrameButton->setEnabled(true);
			m_LastFrameButton->setEnabled(true);
			m_NextAnimButton->setEnabled(true);
			m_LastAnimButton->setEnabled(true);
			m_AnimChooseComboBox->setEnabled(true);
			m_AnimChooseLabel->setEnabled(true);
			m_AnimSpeedEditor->setEnabled(true);
			m_AnimSpeedEditor->setReadOnly(false);
			m_AnimSpeedLabel->setEnabled(true);		
		}
	}
}

void ControlPanelPage::resized()
{
	//m_pResetButton->setBounds( getWidth()/3, 4, getWidth()/2, 20 );
	//m_Control_Panel->setBounds( 4, 24, getWidth()-4, getHeight() );

    m_FrameSlider->setBounds(39, 16, 488, 32);
    m_PauseButton->setBounds(303, 56, 56, 24);
    m_NextFrameButton->setBounds(387, 56, 56, 24);
    m_LastFrameButton->setBounds(216, 56, 56, 24);
    m_NextAnimButton->setBounds(458, 56, 56, 24);
    m_LastAnimButton->setBounds(142, 56, 56, 24);
    m_AnimChooseComboBox->setBounds(632, 22, 100, 24);
    m_AnimChooseLabel->setBounds(536, 21, 96, 24);
    m_AnimSpeedEditor->setBounds(97, 56, 31, 22);
    m_AnimSpeedLabel->setBounds(2, 56, 104, 24);
}

void ControlPanelPage::changeListenerCallback(ChangeBroadcaster*)
{
    /*m_Custom_Panel->refreshAll();*/

}

void ControlPanelPage::sliderValueChanged(Slider* sliderThatWasMoved)
{
	if( sliderThatWasMoved = m_FrameSlider )
	{
		CModelEditorConfig::GetInstance()->m_bFrame0Render.set(0);
		if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MD2 )
		{
			CModelEditorConfig::GetInstance()->m_CurrentFrameID = static_cast<int>(m_FrameSlider->getValue());
			CModelEditorConfig::GetInstance()->m_UIStartFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
			CModelEditorConfig::GetInstance()->m_UIEndFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
		}
		else if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MS3D )
		{
			CModelEditorConfig::GetInstance()->m_CurrentAnimTime = static_cast<float>(m_FrameSlider->getValue());
			CModelEditorConfig::GetInstance()->m_UIStartTime.set( CModelEditorConfig::GetInstance()->m_CurrentAnimTime );
			CModelEditorConfig::GetInstance()->m_UIEndTime.set( CModelEditorConfig::GetInstance()->m_CurrentAnimTime );
		}
	}
}

void ControlPanelPage::comboBoxChanged(ComboBox* comboBoxThatHasChanged)
{
}

void ControlPanelPage::visibilityChanged()
{ 
	if( isVisible() )
	{
		//g_currentTabName = L"Custom";
		//m_pMainComponent->setTextureSampleStatePropertyComponentEnable(true);
	}
}

void ControlPanelPage::ResetUI()
{
	CModelEditorConfig::GetInstance()->m_bLoopPlayMode = false;
	CModelEditorConfig::GetInstance()->m_CurrentFrameID = 0;
	CModelEditorConfig::GetInstance()->m_UIStartFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
	CModelEditorConfig::GetInstance()->m_UIEndFrameID.set( CModelEditorConfig::GetInstance()->m_CurrentFrameID );
	CModelEditorConfig::GetInstance()->m_CurrentAnimID = 0xFFFFFFFF;

	CModelEditorConfig::GetInstance()->m_CurrentAnimTime = 0;
	CModelEditorConfig::GetInstance()->m_UIStartTime.set(CModelEditorConfig::GetInstance()->m_CurrentAnimTime);
	CModelEditorConfig::GetInstance()->m_UIEndTime.set(CModelEditorConfig::GetInstance()->m_CurrentAnimTime);

	if( !g_pCurrentModelFile )
	{
		m_FrameSlider->setEnabled(false);
		m_PauseButton->setEnabled(false);
		m_NextFrameButton->setEnabled(false);
		m_LastFrameButton->setEnabled(false);
		m_NextAnimButton->setEnabled(false);
		m_LastAnimButton->setEnabled(false);
		m_AnimChooseComboBox->setEnabled(false);
		m_AnimChooseLabel->setEnabled(false);
		m_AnimSpeedEditor->setEnabled(false);
		m_AnimSpeedEditor->setReadOnly(true);
		m_AnimSpeedLabel->setEnabled(false);

		m_FrameSlider->setRange(0, 1, 1);
		m_FrameSlider->setValue(0, false, false);
		m_PauseButton->setButtonText(L"|>");
		m_AnimSpeedEditor->setText(L"1.0");
	}
	else
	{
		m_FrameSlider->setEnabled(true);
		m_PauseButton->setEnabled(true);
		m_NextFrameButton->setEnabled(true);
		m_LastFrameButton->setEnabled(true);
		m_NextAnimButton->setEnabled(true);
		m_LastAnimButton->setEnabled(true);
		m_AnimChooseComboBox->setEnabled(true);
		m_AnimChooseLabel->setEnabled(true);
		m_AnimSpeedEditor->setEnabled(true);
		m_AnimSpeedEditor->setReadOnly(false);
		m_AnimSpeedLabel->setEnabled(true);
		m_PauseButton->setButtonText(L"|>");
		m_AnimSpeedEditor->setText(L"1.0");

		if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MD2 )
		{
			CMd2 *pMD2Model = static_cast<CMd2*>(g_pCurrentModelFile);
			pMD2Model->GetFrameNum(NULL, m_SliderFramesStart, m_SliderFramesEnd);		
			m_FrameSlider->setRange(m_SliderFramesStart, m_SliderFramesEnd, 1);
		}
		else if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MS3D )
		{
			CMs3d *pMS3DModel = static_cast<CMs3d*>(g_pCurrentModelFile);
			pMS3DModel->GetAnimTime(NULL, m_SliderTimeStart, m_SliderTimeEnd);
			m_FrameSlider->setRange(m_SliderTimeStart, m_SliderTimeEnd, (m_SliderTimeEnd-m_SliderTimeStart)*0.01f);
		}
		m_FrameSlider->setValue(0, false, false);
	}
}

void ControlPanelPage::textEditorTextChanged(TextEditor& editor)
{
	if( !g_pCurrentModelFile )
		return;

	if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MD2 )
	{
		float fSpeedScale = editor.getText().getFloatValue() * 30;
		if( (fSpeedScale > 0) && (fSpeedScale < 1000) )
			CModelEditorConfig::GetInstance()->m_fFrames_per_second.set(fSpeedScale);
	}
	else if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MS3D )
	{
		float fSpeedScale = editor.getText().getFloatValue();
		if(fSpeedScale > 0)
			CModelEditorConfig::GetInstance()->m_fAnimPlayingSpeed.set(fSpeedScale);
	}
}

void ControlPanelPage::textEditorEscapeKeyPressed(TextEditor& editor)
{
}

void ControlPanelPage::textEditorFocusLost(TextEditor& editor)
{
}

void ControlPanelPage::textEditorReturnKeyPressed(TextEditor &editor)
{
}

void ControlPanelPage::timerCallback()
{
	if( CModelEditorConfig::GetInstance()->m_bLoopPlayMode && g_pCurrentModelFile )
	{
		if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MD2 )
			m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentFrameID, false, false);
		else if( g_pCurrentModelFile->GetModelType() == CModel::EModel_MS3D )
			m_FrameSlider->setValue(CModelEditorConfig::GetInstance()->m_CurrentAnimTime, false, false);
	}
}